﻿using MCUPic.Core;
using MCUPic.Core.Functions.BulkConvert;
using MCUPic.Core.Pic;
using Microsoft.WindowsAPICodePack.Dialogs;
using PReactive.Events;
using PReactive.Ioc;
using PReactive.Mvvm;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Data;

namespace MCUPic.ViewModels
{
    internal class MainViewModel : BindableBase
    {
        //[DependIn]
        //public IPicConverter PicConverter { get; set; }
        CommonOpenFileDialog outputPathDialog;

        public IEnumerable<string> PicPaths { get; set; }

        public DelegateCommand BulkConvertCommand { get; set; }
        public DelegateCommand<string> SelectPicCommand { get; set; }
        public DelegateCommand OutputPathCommand { get; set; }

        private string _outputPath;
        private int _outputWidth;
        private int _outputHeight;
        private int _cutPosX;
        private int _cutPosY;
        private PixelFormat _pixelFormat;
        private ScaleMode _scaleMode;
        public string OutputPath
        {
            get { return _outputPath; }
            set { _outputPath = value; RaisePropertyChanged(nameof(OutputPath)); }
        }
        public string TextBox_OutputWidth
        {
            get { return _outputWidth.ToString(); }
            set
            {
                if (String.IsNullOrEmpty(value)) return;
                _outputWidth = int.Parse(value);
                RaisePropertyChanged(nameof(TextBox_OutputWidth)); 
            }
        }
        public string TextBox_OutputHeight
        {
            get { return _outputHeight.ToString(); }
            set
            {
                if (String.IsNullOrEmpty(value)) return;
                _outputHeight = int.Parse(value); RaisePropertyChanged(nameof(TextBox_OutputHeight));
            }
        }
        public string TextBox_CutPosX
        {
            get { return _cutPosX.ToString(); }
            set
            {
                //if (String.IsNullOrEmpty(value)) return;
                _cutPosX = int.Parse(value); RaisePropertyChanged(nameof(TextBox_CutPosX));
            }
        }
        public string TextBox_CutPosY
        {
            get { return _cutPosY.ToString(); }
            set
            {
                //if (String.IsNullOrEmpty(value)) return;
                _cutPosY = int.Parse(value); RaisePropertyChanged(nameof(TextBox_CutPosY));
            }
        }
        public int PixelFormatIndex
        {
            get 
            {
                switch (_pixelFormat)
                {
                    case PixelFormat.Format16bppRgb565:
                        return 0;
                    case PixelFormat.Format24bppRgb:
                        return 1;
                    default:
                        return 0;
                }
            }
            set
            {
                switch (value)
                {
                    case 0:
                        _pixelFormat = PixelFormat.Format16bppRgb565; break;
                    case 1:
                        _pixelFormat = PixelFormat.Format24bppRgb; break;
                    default:
                        break;
                }
                RaisePropertyChanged(nameof(PixelFormat));
            }
        }
        public int ScaleMode 
        { 
            get
            {
                return (int)_scaleMode;
            }
            set { _scaleMode = (ScaleMode)value; }
        }
        public bool CheckCCode { get;set; }
        public bool CheckHex { get; set; }
        public bool CheckBin { get; set; }
        public bool MSBF { get; set; }

        public MainViewModel(IContainerProvider container)
        {
            InitCommand();
            outputPathDialog = new CommonOpenFileDialog();
            outputPathDialog.IsFolderPicker = true;
            outputPathDialog.InitialDirectory = OutputPath;

            _outputHeight = 64;
            _outputWidth = 128;
            _cutPosY = 0;
            _cutPosX = 0;
            _pixelFormat = PixelFormat.Format16bppRgb565;
            _scaleMode = Core.ScaleMode.FullFill;
            CheckBin = true;

        }

        private void InitCommand()
        {
            BulkConvertCommand = new DelegateCommand(BulkConvert);
            SelectPicCommand = new DelegateCommand<string>(SelectPic);
            OutputPathCommand = new DelegateCommand(SelectOutputPath);
        }

        private void BulkConvert()
        {
            if (OutputPath == null)
            {
                MessageBox.Show("OutputPath is Empty");
                return;
            }
            if (PicPaths == null || PicPaths.Count() == 0)
            {
                MessageBox.Show("PicGroup is Empty");
                return;
            }
            if (!CheckBin && !CheckCCode && !CheckHex)
            {
                MessageBox.Show("Do Nothing");
                return;
            }
            BulkConvertParam param = new BulkConvertParam
            {
                OutputPath = _outputPath,
                PixelFormat = _pixelFormat,
                ScreenWidth = _outputWidth,
                ScreenHeight = _outputHeight,
                Files = PicPaths.ToArray(),
                CutPosX = _cutPosX,
                CutPosY = _cutPosY,
                ScaleMode = _scaleMode,
                OutputOption = new bool[] { CheckCCode, CheckHex, CheckBin, MSBF }
            };
            BulkConvert bc = new BulkConvert(param);

            bc.Start();
        }

        private void SelectPic(string obj)
        {
            var picPathArray = PicPaths?.ToArray();
        }

        private void SelectOutputPath()
        {
            var res =  outputPathDialog.ShowDialog();
            if(res == CommonFileDialogResult.Ok)
            {
                OutputPath = outputPathDialog.FileName;
            }
        }
    }
}
